﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Jsons;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Framework.Verifications;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Weblications.Sango.Environment;
using Pixysoft.Web;
using Pixysoft.Weblications.Sango.Command.general;

namespace Pixysoft.Weblications.Sango.Command.player
{
    partial class SangoPlayerHandler
    {
        public player_create player_create(string loginname, string nickname, string avatar, double geox, double geoy)
        {
            if (!Verification.Verifies(loginname, nickname, avatar).IsNotNullOrEmpty().Result())
                throw Exceptions.VerificationFailedException(loginname, nickname, avatar);

            loginname = loginname.Trim().ToUpper();
            nickname = nickname.Trim().ToUpper();
            long avatarcode = long.Parse(avatar.Trim());

            player_create response = ReflectionManager.CreatePojo<player_create>();
            response.Succeed = false;

            Entities.Usr_Profile profile = UsrProfileDao.tryGetProfileByLoginName(loginname);
            if (profile != null)
            {
                response.Message = "用户信息已经存在";
                return response;
            }

            profile = UsrProfileDao.tryGetProfileByNickname(nickname);
            if (profile != null)
            {
                response.Message = "用户昵称已经存在";
                return response;
            }

            if (!PlayerFormular.isValidNickname(nickname))
            {
                response.Message = "用户昵称错误";
                return response;
            }

            Entities.Sys_Officer sysofficer = SysOfficerHelper.searchByAvatarcode(avatarcode);
            if (!SettingHelper.BooleanValue(sysofficer.Ischief))
            {
                response.Message = "内部错误 非主角角色";
                return response;
            }

            Entities.Sys_Section section = SysSectionHelper.trySearchSectionByGeolocation(geox, geoy);
            if (section == null)
            {
                response.Message = "您所在的区域 目前没有开通此游戏服务";
                return response;
            }

            IEntityNoebeTransaction command = SettingHelper.EntityTransaction;

            command.Begin(delegate()
            {
                //创建用户信息

                profile = UsrProfileDao.createUsrProfile(avatarcode, loginname, nickname);
                command.Insert(profile);


                Entities.Usr_Activity activity = UsrActivityDao.createHistory(profile);
                command.Insert(activity);

                // 初始化用户建造队列

                List<Entities.Usr_Queue> queues = UsrQueueDao.initializeQueues(profile.Usercode, nickname);
                command.Insert(queues.ToArray());

                // 初始化用户建筑

                List<Entities.Usr_Building> buildings = UsrBuildingDao.initializeBuildings(profile.Usercode, loginname);
                command.Insert(buildings.ToArray());

                // 初始化默认将领

                Entities.Usr_Officer usrofficer = OfficerDao.createUserOfficer(profile.Usercode, profile.Nickname, sysofficer);
                usrofficer.Officername = profile.Nickname;

                Entities.Usr_Property property = UsrPropertyDao.createProperty(profile);
                OfficerDao.hireOfficer(usrofficer, property);

                command.Insert(usrofficer);
                command.Insert(property);

                // 建立初始化区域

                Entities.Usr_Section usrsection = UsrSectionDao.CreateSection(section, profile, geox, geoy);
                command.Insert(usrsection);


                // 默认阵法

                Entities.Sys_Tactical systactical = SysTacticalHelper.trySearchByLevel(profile.Userlevel);
                Entities.Usr_Tactical usrtactical = TacticalDao.createUsrTactical(systactical, profile);
                usrtactical.Isdefault = SettingHelper.IntValue(true);
                List<Entities.Usr_Tacticalpos> usrpos = TacticalDao.createUsrTacticalPositions(profile.Userlevel, usrtactical, systactical);
                TacticalDao.initializeDefaultPosition(usrpos, usrofficer);
                command.Insert(usrtactical);
                command.Insert(usrpos.ToArray());

                // 战略表

                foreach (Entities.Cti_Strategy orm in CityStrategyHelper.searchOpenStrategies(PlayerFormular.default_startlevel_player))
                {
                    Entities.Usr_Strategy strategy = StrategyDao.createStrategy(profile, orm);
                    if (usrtactical.Tacticaltypecode == strategy.Strategytypecode)
                        strategy.Strategylevel = PlayerFormular.default_tacticallevel_strategy;
                    command.Insert(strategy);
                }

                // 任务系统

                command.Insert(MissionDao.createMission(profile.Usercode, profile.Nickname));
                command.Insert(MissionDao.initializeMainmissions(profile.Usercode, profile.Nickname).ToArray());

                // 开始关卡

                Entities.Usr_Map usrmap = StageDao.createFirstMap(profile);
                command.Insert(usrmap);

                // 注册日志 TODO

                command.Commit();

            });

            //内存注册用户

            SessionHelper.Instance.AddSession(StringHelper.session_profile, profile.Usercode);

            //通知好友上线


            response.Succeed = true;
            return response;
        }
    }

    public interface player_create : ISangoResponse
    {
    }
}
